package com.jiang.jetpackdemo.fragment

import android.app.AlertDialog
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.jiang.jetpackdemo.R
import com.jiang.jetpackdemo.activity.PlayVideoActivity
import com.jiang.jetpackdemo.adapter.FavoritesAdapter
import kotlinx.android.synthetic.main.favorites_pic_fragment.*
import kotlinx.android.synthetic.main.music_recommend_fragment.*

class FavoritesPicFragment(private val type: Type) : Fragment() {
    enum class Type {
        VIDEO, PICTURE, NEWS
    }

    companion object {
        fun newInstance(type: Type) = FavoritesPicFragment(type)
        const val TAG = "FavoritesPicFragment"
    }

    private val viewModel: FavoritesPicViewModel by lazy {
        ViewModelProvider(this).get(
            FavoritesPicViewModel::class.java
        )
    }
    private val adapter by lazy {
        FavoritesAdapter(
            requireContext(),
            viewModel.getModelList(),
            type
        )
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.favorites_pic_fragment, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initView()
        initViewModel()
        viewModel.getData(type, onFinish = { iv_empty.visibility = View.GONE })

    }

    private fun initViewModel() {
        viewModel.initRoom(requireContext())
        viewModel.sizeOfFavoritesModel.observe(
            viewLifecycleOwner,
            Observer { adapter.notifyDataSetChanged() })
    }

    private fun initView() {
        rv_fav.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
        rv_fav.adapter = adapter
        adapter.setOnLongClick {
            AlertDialog.Builder(context)
                .setTitle("要删除吗？")
                .setPositiveButton("确认") { dialog, which -> viewModel.cancelPic(it) }
                .setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
                .show()
        }

        adapter.setOnClick {
            when (type) {
                Type.VIDEO -> {
                    Toast.makeText(context, "$it", Toast.LENGTH_SHORT).show()
                    val intent = Intent(requireContext(), PlayVideoActivity::class.java)
                    val resultTran = viewModel.resultTran(it)
                    intent.putExtra(VideoRecommendFragment.INTENT_KEY, resultTran)
                    Log.d(TAG, resultTran)
                    startActivity(intent)

                }
                else -> {
                }
            }
        }
        fabtn.setOnClickListener { rv_fav.scrollToPosition(0) }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        Log.d(TAG, "onDestroyView: ")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy")
    }
}